package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierDao supplierDao;

    @Override
    public Map<String, Object> selectSupplierPage(Integer page, Integer rows, String supplierName) {
        Map<String, Object> map = new HashMap<>();

        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;

        List<Supplier> suppliers = supplierDao.getsupplier(rows,offSet,supplierName);
        map.put("total",supplierDao.getSupplierCount(supplierName));
        map.put("rows",suppliers);

        return map;
    }

    @Override
    public ServiceVO saveOrUpdateSupplier(Supplier supplier) {
        if (supplier.getSupplierId() == null){
            //如果供应商id不存在则为新增，判断是否存在该供应商名称
            Supplier exSupplier = supplierDao.findSupplierByName(supplier.getSupplierName());
            if (exSupplier !=null){
                return new ServiceVO(200,"该供应商已经存在");
            }
            supplierDao.insertSupplier(supplier);
        }else {
            supplierDao.updateSupplier(supplier);
        }
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO deleteSupplier(String[] split) {
        Integer count = supplierDao.deleteSupplier(split);
        if (count > 0){
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        }
        return null;
    }




}
